import 'package:flutter/material.dart';
import '../../routers/application.dart';
import '../../api/api_service.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';
import 'package:provide/provide.dart';
import '../../provide/userProvide.dart';
import '../../routers/routers.dart';

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  TextEditingController _username = TextEditingController();
  TextEditingController _password = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Color.fromRGBO(244, 245, 245, 1.0),
        appBar: AppBar(title: Text('登录')),
        body: Center(
            child: ListView(
          padding: EdgeInsets.only(left: 40.0, right: 40.0),
          children: <Widget>[
            Container(
              padding: EdgeInsets.only(top: 34.0, bottom: 46.0),
              child: Text("校园共享单车",
                  textAlign: TextAlign.center,
                  style: new TextStyle(
                      fontSize: 30.0,
                      fontWeight: FontWeight.w900,
                      fontFamily: "Georgia",
                      color: Colors.pink)),
            ),
            TextField(
                keyboardType: TextInputType.emailAddress,
                autofocus: false,
                controller: _username,
                decoration: InputDecoration(
                    contentPadding: EdgeInsets.all(10.0),
                    hintText: '请输入账号',
                    labelText: "请输入账号")),
            TextField(
                keyboardType: TextInputType.emailAddress,
                autofocus: false,
                obscureText: true,
                controller: _password,
                decoration: InputDecoration(
                    contentPadding: EdgeInsets.all(10.0),
                    hintText: '请输入密码',
                    labelText: "请输入密码")),
            Container(
              margin: EdgeInsets.only(top: 29.0),
              child: RaisedButton(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(18),
                ),
                onPressed: onSubmit,
                padding: EdgeInsets.all(7),
                color: Color.fromRGBO(255, 170, 13, 1),
                child: Text('登录',
                    style: TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                        fontSize: 20.0,
                        letterSpacing: 25.0)),
              ),
            ),
          ],
        )));
  }

  void onSubmit() async {
    try {
      await loginApi({
        'username': _username.text.toString().trim(),
        'phone': _username.text.toString().trim(),
        'password': _password.text.toString()
      }).then((data) async {
        if (data['success']) {
          print('login success');
          SharedPreferences prefs = await SharedPreferences.getInstance();
          prefs.setString('eleToken', data['token']);
          prefs.setString('userInfo', json.encode(data['result']).toString());
          Provide.value<UserProvide>(context).changeUserInfo(data['result']);
          Application.router.navigateTo(context, Routes.root, clearStack: true);
          // Application.router.
        } else {
          showDialog(
              context: context,
              builder: (_) => new AlertDialog(content: new Text(data['msg'])));
        }
      });
    } catch (e) {
      print('登录异常');
      print(e);
    }
  }
}
